Answer:

Yes.

IOException

The file myData.txt should exist and should be in the same directory as the program. If the file does not exist, the program cannot construct a Scanner that reads from it. If this happens, the run-time Java system will throw an IOException. In practical terms, this means some error messages will be written and the program will halt.

C:\Temp>java EchoSquareDisk
Exception in thread "main" java.io.FileNotFoundException: myData.txt (The syste
m cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.(Unknown Source)
        at java.util.Scanner.(Unknown Source)
        at EchoSquareDisk.main(EchoSquareDisk.java:9)

Because of this, two things are needed in the program. The main() method must start with the line:

public static void main (String[] args) throws IOException

And the Java io package must be imported:

import java.io.*;

Later chapters further discuss exceptions and what to do with them. For now, expect your program to halt if it cannot find the disk file or if something else goes wrong with I/O.

QUESTION 5:

Is a program that reads a single integer from a file very practical?